/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.api.adwords.starterkit.utils;

import org.apache.log4j.Logger;

import java.util.HashSet;

/**
 * The Class AdTemplate represents a TextAd on the framework.
 * 
 * @author rodrigo.f.vale@gmail.com
 */
public class AdTemplate {
  private String head;
  private String line1;
  private String line2;
  private String visibleUrl;
  private String destUrl;
  private HashSet<String> tags = new HashSet<String>();
  private static final Logger log = Logger.getLogger("StarterKit");
  
  /**
   * Instantiates a new ad template.
   *
   * @param head the TextAd head
   * @param line1 the TextAd line1
   * @param line2 the TextAd line2
   * @param tags the TextAd tags
   * @param visibleUrl the TextAd visible url
   * @param destUrl the TextAd destination url
   */
  public AdTemplate(final String head, final String line1, final String line2, 
      final HashSet<String> tags, final String visibleUrl, 
      final String destUrl) {
    this.head = head;
    this.line1 = line1;
    this.line2 = line2;
    this.tags = tags;
    this.destUrl = destUrl;
    this.visibleUrl = visibleUrl;
  }
  
  /**
   * Replace all keys in the TextAd fields by the correspondent value
   *
   * @param key the regexp key
   * @param value the value to replace
   * @return false, if any of the fields size are against AdWords policy
   */
  public boolean replaceAll(String key, String value) {
    head = head.replace(key, value);
    line1 = line1.replace(key, value);
    line2 = line2.replace(key, value);
    
    if ((head.length() > 25) || (line1.length() > 35) 
        || (line2.length() > 35)) {
      return false;
    }
    return true;
  }
  
  /**
   * Replace all keys in the TextAd fields by the correspondent value
   *
   * @param key the regexp key
   * @param value the value to replace
   * @return false, if any of the fields size are against AdWords policy
   */
  public boolean replaceAllDesc1(String key, String value) {
    try {
      line1 = line1.replace(key, value);
    } catch (Exception e) {
      log.debug("Error replacing AdTemplate.");
      log.debug("Line:" + line1);
      log.debug("Key:" + key);
      log.debug("Value:" + value);
    }
    
    if ((head.length() > 25) || (line1.length() > 35) 
        || (line2.length() > 35)) {
      return false;
    }
    return true;
  }
  
  /**
   * Replace all keys in the TextAd fields by the correspondent value
   *
   * @param key the regexp key
   * @param value the value to replace
   * @return false, if any of the fields size are against AdWords policy
   */
  public boolean replaceAllDesc2(String key, String value) {
    try {
      line2 = line2.replace(key, value);
    } catch (Exception e) {
      log.debug("Error replacing AdTemplate.");
      log.debug("Line:" + line2);
      log.debug("Key:" + key);
      log.debug("Value:" + value);
    }
    
    if ((head.length() > 25) || (line1.length() > 35) 
        || (line2.length() > 35)) {
      return false;
    }
    return true;
  }
  
  /**
   * Gets the TextAd head.
   *
   * @return the head
   */
  public String getHead() {
    return head;
  }
  
  /**
   * Gets the TextAd line1.
   *
   * @return the line1
   */
  public String getLine1() {
    return line1;    
  }

  /**
   * Gets the TextAd line2.
   *
   * @return the line2
   */
  public String getLine2() {
    return line2;    
  }
  
  /**
   * Gets the TextAd tags.
   *
   * @return the tags
   */
  public HashSet<String> getTags() {
    return tags;
  }
  
  /**
   * Gets the TextAd visible url.
   *
   * @return the visible url
   */
  public String getVisibleUrl() {
    return visibleUrl;
  }

  /**
   * Sets the TextAd visible url.
   *
   * @param visibleUrl the new visible url
   */
  public void setVisibleUrl(String visibleUrl) {
    this.visibleUrl = visibleUrl;
  }

  /**
   * Gets the TextAd destination url.
   *
   * @return the dest url
   */
  public String getDestUrl() {
    return destUrl;
  }

  /**
   * Sets the TextAd destination url.
   *
   * @param destUrl the new dest url
   */
  public void setDestUrl(String destUrl) {
    this.destUrl = destUrl;
  }
}
